# Figure 4 code
library(readxl)
library(dplyr)
library(tidyr)
library(ggplot2)

# ==== Parameters ====
setwd("C://Users//gabri//Desktop//base de dados artigo revisão//Figuras")
output_eps <- "Most_Relevant_Countries_SCP_MCP_300dpi.eps"
top_n      <- 20   # number of countries to show (sorted by total)

# ==== Read ====
df_raw <- read_xlsx("Figura_4/Base_figura_4.xlsx", sheet = 1)

View(df_raw)

nm <- names(df_raw)
if (length(nm) < 5) stop("Sheet does not have expected columns.")
names(df_raw)[1:5] <- c("Country","Articles","ArticlesPerc","SCP","MCP")

df <- df_raw %>%
  filter(!is.na(Country)) %>%
  filter(Country != "Country") %>%
  mutate(
    Articles = suppressWarnings(as.numeric(Articles)),
    SCP      = suppressWarnings(as.numeric(SCP)),
    MCP      = suppressWarnings(as.numeric(MCP))
  ) %>%
  filter(!is.na(SCP), !is.na(MCP)) %>%
  mutate(Total = SCP + MCP) %>%
  arrange(desc(Total)) %>%
  slice_head(n = top_n)

# ==== Long format for stacking ====
df_long <- df %>%
  select(Country, SCP, MCP) %>%
  pivot_longer(cols = c(SCP, MCP), names_to = "Type", values_to = "Count")

# Transformando para dataframe
str(df_long)
df_long = as.data.frame(df_long)

# ==== Plot ====
p <- ggplot(df_long,
            aes(x = Count,
                y = reorder(Country, Count, FUN = sum),  # <-- aqui
                fill = Type)) +
  geom_col(position = "stack") +
  labs(x = "N. of Documents", y = "Countries") +
  theme_minimal(base_size = 12) +
  theme(panel.grid.major.y = element_blank())
p

# ==== Save EPS (300 dpi) ====
ggsave(filename = output_eps, plot = p, device = cairo_ps,
       width = 10, height = 6, units = "in", dpi = 300)

cat(paste0("Saved: ", normalizePath(output_eps), "\n"))
